﻿// -----------------------------------------------------------------------
// <copyright file="Subtitle.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace SubtitleFontCollector
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Media;

    public enum SubtitleType
    {
        ASS = 0,
    }

    /// <summary>
    /// subtitle files.
    /// </summary>
    public class Subtitle
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Subtitle" /> class.
        /// </summary>
        /// <param name="filename">file name</param>
        /// <param name="type">sub title type.</param>
        public Subtitle(string filename, SubtitleType type)
        {
            this.Filename = filename;
            this.SubType = type;
        }

        public string Filename
        {
            get;
            private set;
        }

        public SubtitleType SubType
        {
            get;
            private set;
        }
    }

    public class FontUsage
    {
        private FontFamily font;
        private Hashtable chineseHash;

        public FontUsage(string fontName, bool isHeaderStyle, Hashtable hashChinese)
        {
            this.chineseHash = hashChinese;
            this.FontName = fontName;
            this.IsHeaderStyle = isHeaderStyle;
            this.Count = 1;

            this.TryToCreateFont(fontName);
        }

        public string FontName
        {
            get;
            private set;
        }

        public string IdealFontName
        {
            get;
            private set;
        }

        public FontFamily Font
        {
            get
            {
                return this.font;
            }
        }

        public bool IsHeaderStyle
        {
            get;
            private set;
        }

        public bool IsAvailable
        {
            get;
            private set;
        }

        public long Count
        {
            get;
            private set;
        }

        public void Increase()
        {
            this.Count++;
        }

        private bool TryToCreateFont(string providedName)
        {
            string simplifiedChineseName = providedName;
            string traditionalChineseName = providedName;
            if (this.chineseHash.Contains(providedName))
            {
                string storeValue = this.chineseHash[providedName].ToString();
                string[] values = storeValue.Split('|');
                if (values.Length == 2)
                {
                    simplifiedChineseName = values[0];
                    traditionalChineseName = values[1];
                }
            }
            else
            {
                simplifiedChineseName = Win32API.ToSimplified(providedName);
                traditionalChineseName = Win32API.ToTraditional(providedName);
                this.chineseHash.Add(providedName, string.Format("{0}|{1}", simplifiedChineseName, traditionalChineseName));
            }

            FontFamily f = this.CreateFont(simplifiedChineseName);
            if (f == null)
            {
                f = this.CreateFont(traditionalChineseName);
            }

            this.font = f;

            this.IsAvailable = f != null;
            return this.IsAvailable;
        }

        private FontFamily CreateFont(string fontName)
        {
            FontFamily f = new FontFamily(fontName);
            if (f.FamilyNames.Values.Any(fn => f.Source.Contains(fn)))
            {
                return f;
            }

            return null;
        }
    }
}
